<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="flex justify-between w-full mb8">
				<el-space>
					<el-popover trigger="click" :width="320" placement="bottom-start">
						<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef" labelWidth="90px" size="small">
							<el-form-item label="设备类型" prop="deviceTypeId">
								<el-select class="w100" clearable filterable v-model="state.queryForm.deviceTypeId" :teleported="false">
									<el-option :key="item.id" :label="item.typeName" :value="item.id" v-for="item in deviceTypeList" :disabled="item.state === 20" />
								</el-select>
							</el-form-item>
							<el-form-item label="通讯模型" prop="communicationModelId">
								<el-select class="w100" clearable filterable v-model="state.queryForm.communicationModelId" :teleported="false">
									<el-option
										:key="item.id"
										:label="item.name"
										:value="item.id"
										v-for="item in communicationModelList"
										:disabled="item.state === 20"
									/>
								</el-select>
							</el-form-item>
							<!-- <el-form-item label="生产商" prop="deviceModelId">
								<el-select class="w100" clearable filterable v-model="state.queryForm.deviceModelId" :teleported="false">
									<el-option :key="item.id" :label="item.deviceModelName" :value="item.id" v-for="item in deviceModelList" />
								</el-select>
							</el-form-item> -->
							<el-form-item label="运营商" prop="operatorId">
								<el-select class="w100" clearable filterable v-model="state.queryForm.operatorId" :teleported="false">
									<el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in operatorListList" :disabled="item.state === 20" />
								</el-select>
							</el-form-item>
						</el-form>
						<template #reference>
							<el-button link type="primary"
								>高级搜索<el-icon><ArrowDown /></el-icon>
							</el-button>
						</template>
					</el-popover>
					<el-input placeholder="请输入关键字进行查询" v-model="state.queryForm.deviceModelName" />
					<el-button type="primary" @click="getDataList(true)">搜索</el-button>
					<el-button icon="Refresh" @click="resetQuery"></el-button>
				</el-space>

				<div>
					<el-button type="success" class="mr-2" @click="handleRecord"> 授权记录</el-button>
					<!-- 更多操作 -->
					<el-popover :width="160">
						<template #reference>
							<el-button type="primary"
								>更多操作 <el-icon class="ArrowDown"><ArrowDown /></el-icon
							></el-button>
						</template>
						<el-row>
							<el-col :span="24">
								<el-button
									class="ml-3"
									text
									icon="Stamp"
									:disabled="multiple"
									type="primary"
									@click="SubordinateAuthDialogRef.openDialog(selectObjs)"
									v-auth="'platform_basicDeviceStock_subordinateAuth'"
									>下级授权</el-button
								>
								<el-button
									text
									icon="DeleteFilled"
									type="danger"
									@click="handleDelete(selectObjs)"
									:disabled="multiple"
									v-auth="'platform_basicDeviceStock_del'"
								>
									删除型号
								</el-button>
							</el-col>
						</el-row>
					</el-popover>
				</div>
			</div>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="deviceType" label="设备类型" show-overflow-tooltip />
				<el-table-column prop="deviceModelName" label="设备型号" show-overflow-tooltip />
				<el-table-column prop="communicationModelName" label="通讯模型" show-overflow-tooltip />
				<el-table-column prop="operatorName" label="运营商" show-overflow-tooltip />
				<el-table-column prop="deptName" label="授权单位" show-overflow-tooltip />
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip />
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<!-- <form-dialog ref="formDialogRef" @refresh="getDataList(false)" /> -->
		<!-- 平级授权 -->
		<ParallelAuthDialog ref="ParallelAuthDialogRef" @refresh="getDataList(false)" />
		<!-- 下级授权 -->
		<SubordinateAuthDialog ref="SubordinateAuthDialogRef" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="systemBasicDeviceModel">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/platform/basicDeviceStock';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { fetchListAll as fetchCommunicationModelList } from '/@/api/platform/basicCommunicationModel';
import { fetchListAll as fetchOperatorList } from '/@/api/platform/basicOperatorType';
import { fetchListAll } from '/@/api/platform/basicDeviceType';
import router from '/@/router';

// 引入组件
// const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const ParallelAuthDialog = defineAsyncComponent(() => import('./components/parallelAuth.vue'));
const SubordinateAuthDialog = defineAsyncComponent(() => import('./components/subordinateAuth.vue'));
// 定义查询字典

// 定义变量内容
// const formDialogRef = ref();

const ParallelAuthDialogRef = ref();
const SubordinateAuthDialogRef = ref();
const deviceTypeList: any = ref([]);
const communicationModelList: any = ref([]);
const operatorListList: any = ref([]);
// 搜索变量
const queryRef = ref();
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.deviceModelName = '';
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/basicDeviceModel/export', Object.assign(state.queryForm, { ids: selectObjs }), 'basicDeviceModel.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs;
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (objs: any[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(objs.map((d) => d.id));
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 获取设备型号
const getDeviceTypes = () => {
	fetchListAll({ state: 20 }).then((res) => {
		deviceTypeList.value = res.data;
	});
};
getDeviceTypes();
// 获取通讯模型
const getCommunicationModels = () => {
	fetchCommunicationModelList({ state: 10 }).then((res) => {
		communicationModelList.value = res.data;
	});
};
// 获取运营商
const getOperatorList = () => {
	fetchOperatorList({ state: 10 }).then((res) => {
		operatorListList.value = res.data;
	});
};
getCommunicationModels();
getOperatorList();
// 授权记录
const handleRecord = () => {
	router.push({
		path: '/platform/basicAuthorizationRecord/index',
	});
};
</script>
<style lang="scss"></style>
